#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : 2025/6/30 09:06
# @function: What can I do.
# @version : V1
import tkinter
from tkinter import filedialog
import os


class GetColumn:
    def __init__(self):
        self.RED = '\033[91m'
        self.GREEN = '\033[92m'
        self.END = '\033[0m'

    def read_file(self):
        askdir = self.askdir = filedialog.askdirectory()
        if askdir is not None:
            file_list = self.file_list = os.listdir(askdir)
            file_show = open(os.path.join(askdir, file_list[0]), "r",encoding="utf-8")
            self.t1.insert(tkinter.END, file_show.name)
            sample_line = file_show.readline()
            sample_line_list = sample_line.split()
            file_show.close()
            self.dir_path.set(askdir)
            self.t1.insert(tkinter.END, "序号\t示例\n")
            for i in range(len(sample_line_list)):
                self.t1.insert(tkinter.END, str(i) + '\t' + sample_line_list[i] + "\n")
            self.t1.insert(tkinter.END, "请输入对应的序号\n")
        else:
            self.t1.delete("1.0", tkinter.END)
            self.t1.insert(tkinter.END, "未选择文件")
            pass

    def splitFile(self, event=None):
        self.t1.insert(tkinter.END, "开始分割\n")
        new_dir = filedialog.askdirectory()
        if new_dir is not None:
            for file_name in self.file_list:
                result = ""
                file_name  = os.path.join(self.askdir, file_name)
                self.file_name.set(file_name)
                curent_file = open(file_name, "r")
                lines = curent_file.readlines()
                for row in lines:
                    new_line = row.strip().split()[int(self.e0.get())]
                    result+=new_line+"\n"
                    self.t1.insert(tkinter.END, new_line+"\n")
                    self.t1.see(tkinter.END)
                    self.t1.update()
                print(f"result:{result}")
                print("当前文件处理完成")
                curent_file.close()
                new_file_path = os.path.join(new_dir, os.path.basename(file_name))
                t_f = open(new_file_path, "w")
                t_f.write(result)
                t_f.close()
                self.t1.insert(tkinter.END, "转换完成\n")
                self.t1.see(tkinter.END)
        else:
            self.t1.insert(tkinter.END,"未选择存储位置\n")
            self.t1.see(tkinter.END)
    def write_file(self):
        target_file = self.file_name.get()
        if target_file != "":
            t_f = open(target_file, "w", encoding="utf-8")
            t_f.write(self.result)
            t_f.close()
            self.t1.insert(tkinter.END, "转换完成")
            self.t1.see(tkinter.END)
        else:
            self.t1.insert(tkinter.END, "保存文件未选择")
            self.t1.see(tkinter.END)
            pass

    def mainWindow(self):
        root = tkinter.Tk()
        root.title("Get Column")
        root.resizable(False, False)

        self.dir_path = tkinter.StringVar()
        self.dir_path.set("")
        self.file_name = tkinter.StringVar()
        self.file_name.set("")

        root.bind("<Return>", self.splitFile)
        # root.bind_all("<Control-s>", self.write_file)
        # 第一行
        Frm0 = tkinter.Frame(root)
        tkinter.Label(Frm0, text="选择文件夹").pack()
        tkinter.Entry(Frm0, state="disabled", textvariable=self.dir_path, width=70).pack(fill=tkinter.X)
        tkinter.Label(Frm0,text="当前文件").pack()
        tkinter.Entry(Frm0,state="disabled",textvariable=self.file_name, width=70).pack(fill=tkinter.X)
        Frm0.pack(side=tkinter.TOP, fill=tkinter.X)
        # 第二行（输入）
        Frm1 = tkinter.Frame(root)
        tkinter.Label(Frm1, text="请输入下方序号").pack(side=tkinter.LEFT, padx=5)
        self.e0 = tkinter.Entry(Frm1)
        self.e0.pack(side=tkinter.LEFT, padx=5, fill=tkinter.X, expand=True)
        Frm1.pack(side=tkinter.TOP, fill=tkinter.X)
        # 第三行（指令）
        Frm2 = tkinter.Frame(root)
        tkinter.Button(Frm2, text="Browse", command=self.read_file).pack(side=tkinter.LEFT, padx=5, ipadx=10,
                                                                         anchor=tkinter.CENTER, expand=True)
        # tkinter.Button(Frm2, text="Show", command=self.showFirstRow).pack(side=tkinter.LEFT, padx=5, ipadx=10,
        #                                                                   anchor=tkinter.CENTER, expand=True)
        tkinter.Button(Frm2, text="Split", command=self.splitFile).pack(side=tkinter.LEFT, padx=5, ipadx=10,
                                                                        anchor=tkinter.CENTER, expand=True)
        tkinter.Button(Frm2, text="Save", command=self.write_file).pack(side=tkinter.LEFT, padx=5, ipadx=10,
                                                                        anchor=tkinter.CENTER, expand=True)
        Frm2.pack(side=tkinter.TOP, fill=tkinter.X)
        # 第四行（输出）
        Frm3 = tkinter.Frame(root)
        tkinter.Label(Frm3, text="预览").pack(side=tkinter.TOP, fill=tkinter.X)
        self.t1 = tkinter.Text(Frm3, height=20)
        self.t1.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
        scb = tkinter.Scrollbar(Frm3, command=self.t1.yview)
        scb.pack(side=tkinter.LEFT, fill=tkinter.Y)
        self.t1.configure(yscrollcommand=scb.set)
        Frm3.pack(side=tkinter.TOP, fill=tkinter.X)

        root.mainloop()


myapp = GetColumn()
myapp.mainWindow()
